import { app, BrowserWindow } from "electron";
import path from "path";
import { ChatCompletion } from "@baiducloud/qianfan";

if (require("electron-squirrel-startup")) {
  app.quit();
}

const createWindow = async () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"),
    },
  });

  // and load the index.html of the app.
  if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
    mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
  } else {
    mainWindow.loadFile(
      path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`)
    );
  }
  // mainWindow.webContents.openDevTools();
  const client = new ChatCompletion({
    QIANFAN_ACCESS_KEY: "ALTAKX6VmtWjxMnPSqGo0RWcBk",
    QIANFAN_SECRET_KEY: "ce7cdbf5ccb142a7860962262c91875c",
  });
  const resp = await client.chat(
    {
      messages: [
        {
          role: "user",
          content: "你是谁？",
        },
      ],
    },
    "ERNIE-Lite-8K-0308"
  );
  console.log(resp);
};
app.on("ready", createWindow);

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
